﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/14 22:29:24
 * ************************************************************/
namespace Mozlite
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using System.Collections.Generic;
    using Mozlite.Web;

    /// <summary>
    /// 请求扩展方法类。
    /// </summary>
    public static class RequestUtility
    {
        #region form collection
        /// <summary>
        /// 获取表单数据。
        /// </summary>
        /// <typeparam name="T">枚举类型。</typeparam>
        /// <param name="form">表单集合。</param>
        /// <param name="key">表单元素名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回表单数据。</returns>
        public static T Get<T>(this FormCollection form, string key, T defaultValue = default(T))
        {
            return form[key].ChangeTo<T>(defaultValue);
        }
       
        /// <summary>
        /// 获取POST行为。
        /// </summary>
        /// <param name="form">表单集合。</param>
        /// <returns>返回POST行为。</returns>
        public static HttpAction GetAction(this FormCollection form)
        {
            return form.Get<HttpAction>("action", HttpAction.None);
        }

        /// <summary>
        /// 获取POST行为。
        /// </summary>
        /// <param name="form">路由实例对象。</param>
        /// <returns>返回POST行为。</returns>
        public static HttpAction GetHttpAction(this FormCollection form)
        {
            return form.Get<HttpAction>("httpaction", HttpAction.None);
        }
        #endregion

        #region controller querystring
        /// <summary>
        /// 获取表单数据。
        /// </summary>
        /// <typeparam name="T">枚举类型。</typeparam>
        /// <param name="controller">表单集合。</param>
        /// <param name="key">表单元素名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回表单数据。</returns>
        public static T Get<T>(this Controller controller, string key, T defaultValue = default(T))
        {
            return controller.Request.QueryString[key].ChangeTo<T>(defaultValue);
        }

        /// <summary>
        /// 获取GET行为。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <returns>返回GET行为。</returns>
        public static HttpAction GetAction(this Controller controller)
        {
            return controller.Get<HttpAction>("action", HttpAction.None);
        }

        /// <summary>
        /// 获取GET行为。
        /// </summary>
        /// <param name="controller">路由实例对象。</param>
        /// <returns>返回GET行为。</returns>
        public static HttpAction GetHttpAction(this Controller controller)
        {
            return controller.Get<HttpAction>("httpaction", HttpAction.None);
        }
        #endregion

        #region pagebase querystring
        /// <summary>
        /// 获取表单数据。
        /// </summary>
        /// <typeparam name="T">枚举类型。</typeparam>
        /// <param name="page">表单集合。</param>
        /// <param name="key">表单元素名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回表单数据。</returns>
        public static T Get<T>(this HttpRequestBase request, string key, T defaultValue = default(T))
        {
            return request.QueryString[key].ChangeTo<T>(defaultValue);
        }

        /// <summary>
        /// 获取GET行为。
        /// </summary>
        /// <param name="request">HTTP请求。</param>
        /// <returns>返回GET行为。</returns>
        public static HttpAction GetAction(this HttpRequestBase request)
        {
            return request.Get<HttpAction>("action", HttpAction.None);
        }

        /// <summary>
        /// 获取GET行为。
        /// </summary>
        /// <param name="request">路由实例对象。</param>
        /// <returns>返回GET行为。</returns>
        public static HttpAction GetHttpAction(this HttpRequestBase request)
        {
            return request.Get<HttpAction>("httpaction", HttpAction.None);
        }
        #endregion

        #region routes
        /// <summary>
        /// 获取表单数据。
        /// </summary>
        /// <typeparam name="T">枚举类型。</typeparam>
        /// <param name="route">表单集合。</param>
        /// <param name="key">表单元素名称。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回表单数据。</returns>
        public static T Get<T>(this RouteData route, string key, T defaultValue = default(T))
        {
            try
            {
                return route.GetRequiredString(key).ChangeTo<T>(defaultValue);
            }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 获取路由行为。
        /// </summary>
        /// <param name="route">路由实例对象。</param>
        /// <returns>返回行为。</returns>
        public static HttpAction GetAction(this RouteData route)
        {
            return route.Get<HttpAction>("action", HttpAction.None);
        }

        /// <summary>
        /// 获取路由行为。
        /// </summary>
        /// <param name="route">路由实例对象。</param>
        /// <returns>返回行为。</returns>
        public static HttpAction GetHttpAction(this RouteData route)
        {
            return route.Get<HttpAction>("httpaction", HttpAction.None);
        }
        #endregion
    }
}